<!--图片上传控件，改造自mpvue-weui-->
<template>
  <div class="weui-uploader">
    <div class="weui-uploader__hd" v-if="showTip">
      <div class="weui-uploader__title">图片上传</div>
      <div class="weui-uploader__info">{{files.length}}/{{maxLength}}</div>
    </div>
    <div class="weui-uploader__bd">
      <div class="weui-uploader__files" id="uploaderFiles">
        <div v-for="(item, index) in files" :key="index" class="weui-uploader__file--box">
          <div class="weui-uploader__file">
            <image class="weui-uploader__img" :class="{'weui-uploader__img-2': upstyle === 2}" :src="item" mode="aspectFill" @click="previewImage" :id="item" />
            <div v-if="delIconShow" class="delete-icon" @click="deleteImg" :id="item" :data-index="index" :key="index"></div>
          </div>
        </div>
        <!-- class="weui-uploader__input-box" -->
        <div v-if="(files.length + voucherLength) < maxLength">
          <!-- <div class="weui-uploader__input" @click="chooseImage"></div> -->
          <div class="upload-style font-10" @click="chooseImage" v-if="upstyle === 1">
            <img v-if="fileSpace" :src="fileSpace + '/images/w-icon-camera.png'" class="width-24 height-18" alt="">
            <div class="mar-t-5">上传凭证</div>
            <div>最多{{maxLength}}张</div>
          </div>
          <div class="upload-style-2 font-10" @click="chooseImage" v-else-if="upstyle === 2"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        isComponents: true,
        uploaderIs: true,
        files: [],
        delIconShow: false
      }
    },
    props: {
      uploadFiles: {
        type: Array,
        default: function() {
          return []
        }
      },
      maxLength: {
        type: Number,
        default: 20
      },
      showTip: {
        type: Boolean,
        default: true
      },
      count: {
        type: Number,
        default: 9
      },
      voucherLength: {
        type: Number,
        default: 0
      },
      upstyle: {
        type: Number,
        default: 1
      }
    },
    onLoad() {
      // 初始化data
      Object.assign(this.$data, this.$options.data())
    },
    mounted() {
      this.files = this.files.concat(this.uploadFiles)
    },
    watch: {
      uploadFiles(val) {
        this.files = this.files.concat(this.uploadFiles)
      },
      files(val) {
        if (val.length > 0) {
          this.delIconShow = true
        }
      }
    },
    methods: {
      chooseImage(e) {
        let that = this
        this.uploaderIs = false
        if (!(this.files.length > this.maxLength - 1)) {
          that.$pt.chooseImage({
            count: that.count - that.files.length - that.voucherLength, // 默认9
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
              // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
              that.files = that.files.concat(res.tempFilePaths)
              if (that.files.length + that.voucherLength > that.maxLength) {
                that.files = that.files.slice(0, that.maxLength - that.voucherLength)
              }
              res.files = that.files
              that.$emit('upLoadSuccess', res)
            },
            fail: function(res) {
              that.uploaderIs = true
              that.$emit('upLoadFail', res)
              // 修改提交记录
            },
            complete: function(res) {
              that.$emit('upLoadComplete', res)
            }
          })
        } else {
          that.$utils.showToast({
            title: `最多上传 ${this.maxLength} 张图片`,
            icon: 'none',
            mask: true
          })
        }
      },
      previewImage(e) {
        this.$pt.previewImage({
          current: e.currentTarget.id, // 当前显示图片的http链接
          urls: this.files // 需要预览的图片http链接列表
        })
      },
      deleteImg(e) {
        Array.prototype.indexOf = function(val) { // eslint-disable-line
          for (let i = 0; i < this.length; i++) {
            if (this[i] === val) return i
          }
          return -1
        }
        Array.prototype.remove = function(val) { // eslint-disable-line
          let index = this.indexOf(val)
          if (index > -1) {
            this.splice(index, 1)
          }
        }
        this.files.remove(e.currentTarget.id)
        if (this.files.length === 0) {
          this.uploaderIs = true
        }
        let uploadDeleteObj = {
          src: e.currentTarget.id,
          index: e.currentTarget.dataset.index,
          files: this.files
        }
        this.$emit('uploadDelete', uploadDeleteObj)
      }
    }
  }
</script>

<style lang="less" scoped>
  .weui-uploader__files{
    zoom: 1;
    display: flex;
    &::after{
      content: '';
      clear: both;
      display: block;
    }
  }
  .weui-uploader__file--box{
    &:last-child{
      .weui-uploader__file{
        margin-right: unset;
      }
    }
  }
  .weui-uploader__file {
    position: relative;
  }
  .weui-uploader__bd {
    overflow: visible;
  }
  .upload-style{
    width: 79px;
    height: 79px;
    box-sizing: border-box;
    border: 1px dashed #A9A9A9;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    line-height: 1.2;
    color: #ABABAB;
    margin-bottom: 9px;
  }
  .upload-style-2{
    width: 66px;
    height: 66px;
    background-color: #F8F8F8;
    position: relative;
    margin-bottom: 9px;
    &::before{
      content: '';
      position: absolute;
      width: 14px;
      height: 2px;
      background-color: #999999;
      left: 26px;
      top: 32px;
    }
    &::after{
      content: '';
      position: absolute;
      width: 2px;
      height: 14px;
      background-color: #999999;
      left: 32px;
      top: 26px;
    }
  }
  .weui-uploader__img-2{
    width: 66px;
    height: 66px;
  }
  .delete-icon {
    position: absolute;
    width: 20px;
    height: 20px;
    background: #f43530;
    right: 0;
    top: -10px;
    border-radius: 20px;
    z-index: 5;
  }
  .delete-icon::before {
    content: '';
    width: 13px;
    height: 2px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
  }
</style>
